<?php
namespace Home\Controller;

class SourceController {
	public function _initialize(){

		parent::_initialize();
	}
	public function test(){
		$currency_list=M('Currency')->where('is_line = 1')->select();
		foreach ($currency_list as $k =>$v){
			$this->source($v['currency_url'],$v['currency_id']);
		}
	}
	
	private function source($code,$currency_id){	
		
		$url1m='http://kxian.ronmei.com/chart/chart.php?code='.$code.'&rows=100&interval=1';
		$result1m=$this->aaa($url1m);
		$filename1m = __APP__."KlineData/KLineData_kline_1_".$currency_id.".js";
		file_put_contents($filename1m, $result1m);
		$url5m='http://kxian.ronmei.com/chart/chart.php?code='.$code.'&rows=100&interval=5';
		$result5m=$this->aaa($url5m);
		$filename5m = __APP__."KlineData/KLineData_kline_2_".$currency_id.".js";
		file_put_contents($filename5m, $result5m);
		$url30m='http://kxian.ronmei.com/chart/chart.php?code='.$code.'&rows=100&interval=30';
		$result30m=$this->aaa($url30m);
		$filename30m = __APP__."KlineData/KLineData_kline_3_".$currency_id.".js";
		file_put_contents($filename30m, $result30m);
		$url1h='http://kxian.ronmei.com/chart/chart.php?code='.$code.'&rows=100&interval=60';
		$result1h=$this->aaa($url1h);
		$filename1h = __APP__."KlineData/KLineData_kline_4_".$currency_id.".js";
		file_put_contents($filename1h, $result1h);
		$url1d='http://kxian.ronmei.com/chart/chart.php?code='.$code.'&rows=100&interval=1440';
		$result1d=$this->aaa($url1d);
		$filename1d = __APP__."KlineData/KLineData_kline_5_".$currency_id.".js";
		file_put_contents($filename1d, $result1d);
		
	}
	
	private function aaa($url){
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL,$url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
		$result = curl_exec($ch);
		curl_close ($ch);
		$newresult=json_decode($result);
		foreach ($newresult as $k=>$v){
			array_pop($newresult[$k]);
		}
		$resultfinal=json_encode($newresult);
		return $resultfinal;
	}
	
	public function result($currency_id){
		$currencyUrl=M('Currency')->where('currency_id ='.$currency_id)->find()['currency_url'];
		//$url = "http://data.ronmei.com:8888/Price?code=CL1712";
		$url = "http://data.ronmei.com:8888/Price?code=".$currencyUrl;
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL,$url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
		$result = curl_exec($ch);
		curl_close ($ch);
		$newresult=json_decode($result);
		return $newresult;
	}
}